﻿var paginationTotal = 1;//记录总数
$(function () {
//    initData(0); //初始化分页<{$lang.data}>
    $("#dialog").dialog({
        autoOpen:false,
        modal:true,
        width:380,
        show:"slide",
        buttons:{
            '<{$lang.ok}>':function () {
                //<{$lang.edit}><{$lang.product}>ID <{$lang.tip}>
                productid=$("[name=product_id]","#editdataForm").val();
                c_productid=$("[name=c_product_id]","#editdataForm").val();
                if(productid!=c_productid){
                $('<div title="<{$lang.tip}>" class="msg"><p align="center"><{$lang.ok}>要<{$lang.edit}><{$lang.product}>ID ？</p></div>').dialog({
                        modal:true,
                        buttons:{
                            '<{$lang.ok}>':function () {
                                editdata();
                                $(this).dialog("close");
                            },
                            '<{$lang.cancel}>':function () {
                                $(this).dialog("close");
                            }
                        }
                 });
                }else{
                    editdata();
                }
            },
            '<{$lang.cancel}>':function () {
                $(this).dialog('close');
            }
        },
        close:function () {
            tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
            $('#editdataForm')[0].reset();
        }
    });



    $("#dialogshelf").dialog({
        autoOpen:false,
        modal:true,
        width:380,
        show:"slide",
        buttons:{
            '<{$lang.ok}>':function () {
                editshlefdata();
            },
            '<{$lang.cancel}>':function () {
                $(this).dialog('close');
            }
        },
        close:function () {
            tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
            $('#editshelfForm')[0].reset();
        }
    });

    $("#dialog-tabs").dialog({
        autoOpen:false,
        modal:true,
        width:680,
        show:"slide"
    });
});

function search() {
    initData(0);
}

function loadData(page, pageSize) {
    var ws_code = $('[name=ws_code]').val();
    var putaway_code = $('[name=putaway_code]').val();
    var receiving_code = $('[name=receiving_code]').val();
	var product_sku=$('[name=product_sku]').val();
	var customer_id=$('[name=customer_id]').val();
	
    page = page < 1 ? 1 : page;
    $.ajax({ //这里使用到Jquery的ajax<{$lang.method}>
        type:"POST",
        async:false,
        dataType:"json",
        url:"/warehouse/productsbatch/list/page/" + page + "/pageSize/" + pageSize, //请求的处理url
        data:{
            'page':page, 'pageSize':pageSize,
            'ws_code':ws_code,
            'putaway_code':putaway_code,
            'receiving_code':receiving_code,
			'product_sku':product_sku,
			'customer_id':customer_id
        },
        success:function (json) {
            var list = '';
			paginationTotal = json.total;
            if (json.ask != 1) {
                list = "<td colspan='12' class=''>&nbsp;<{$lang.no_data}></td>";
            } else {
                var i = page == 1 ? 1 : pageSize * (page - 1) + 1;
                $.each(json.result, function (key, val) {
                    list += "<tr>";
                    list += "<td>" + val.wpb_id + "</td>";
                    list += "<td>" + val.ws_code + "</td>";
                    list += "<td>" + val.putaway_code + "</td>";
					if(val.wpb_status==0){
					   list += "<td>" + "<{$lang.unuseable}>" + "</td>";
					}else{
					   list += "<td>" + "<{$lang.useable}>" + "</td>";
					}
                    
                    list += "<td>" + val.receiving_code + "</td>";
                    list += "<td>" + val.customer_id + "</td>";
                    list += "<td>" + val.customer_id+"-"+val.product_sku + "</td>";
                    list += "<td>" + val.wpb_quantity + "</td>";
                    list += "<td>" + val.wpb_quantity_hold + "</td>";
                    list += "<td>" + val.wpb_create_time + "</td>";
                    list += "<td>" + val.wpb_last_update + "</td>";
                    list += "<td><a href=\"javascript:edit(" + val.wpb_id + ");\"><img title='<{$lang.edit}>' src='/images/icon/edit.gif'></a><a href=\"javascript:editShelf(" + val.wpb_id + ");\"><img title='货位<{$lang.adjust}>' src='/images/icon/icon_edit.gif'></a>";
                    list += "</tr>";
                });
            }
			 $("#list").html(list);
        }
    });
}

function tip(mess) {
    var fid = $("#tips");
    fid.html(mess);
    if (mess != '') {
        fid.show();
    } else {
        fid.hide();
    }
}

//<{$lang.submit}><{$lang.inventory}><{$lang.adjust}>
function editdata() {
    vForm = "";
    vForm = $("#editdataForm").serializeArray();
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/warehouse/productsbatch/editdata",
        data:vForm,
        success:function (json) {
            var html = "";
            if (json == null)return;
            html=json.msg;
            if (json.ask) {
                $("#dialog").dialog("close");
                search();
            }
            alertTip(html);
        }
    });
}

//<{$lang.submit}>货位<{$lang.edit}>
function editshlefdata() {
    vForm = "";
    vForm = $("#editshelfForm").serializeArray();
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/warehouse/productsbatch/editshlef",
        data:vForm,
        success:function (json) {
            var html = '';
            if (json == null)return;
                html=json.msg;
            if (json.ask) {
                $("#dialogshelf").dialog("close");
                search();
            }
            alertTip(html);
        }
    });
}

//<{$lang.inventory}><{$lang.adjust}>治
function edit(id) {
    tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/warehouse/productsbatch/info",
        data:"wpb_id=" + id,
        success:function (json) {
            if (json == null) {
                return;
            }
            if (json.ask == 1) {
                $("[name=wpb_id]","#editdataForm").val(json.result.wpb_id);
                $("[name=putaway_code]","#editdataForm").val(json.result.putaway_code);
                $("[name=wpb_quantity]","#editdataForm").val(json.result.wpb_quantity);
                $("[name=putaway_lot_number]","#editdataForm").val(json.result.putaway_lot_number);
                $("[name=product_id]","#editdataForm").val(json.result.product_id);
                $("[name=c_product_id]","#editdataForm").val(json.result.product_id);
                $("#dialog").dialog({title:'<{$lang.inventory_change}>', autoOpen:true});
            }
        }
    });
}


//货位<{$lang.adjust}>
function editShelf(id) {
    tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
    $.ajax({
        type:"post",
        async:false,
        dataType:"json",
        url:"/warehouse/productsbatch/shelfinfo",
        data:"wpb_id=" + id,
        success:function (json) {
            if (json == null) {
                return;
            }
            if (json.ask == 1) {
                $("[name=wpb_id]","#editshelfForm").val(json.result.wpb_id);
                $("[name=ws_code_old]").val(json.result.ws_code);
                $("[name=putaway_code]","#editshelfForm").val(json.result.putaway_code);
                $("[name=move_quantity]","#editshelfForm").attr('oldvalue',json.result.wpb_quantity).val(json.result.wpb_quantity);
                $("[name=putaway_lot_number]","#editshelfForm").val(json.result.putaway_lot_number);
                $("[name=product_id]","#editshelfForm").val(json.result.product_id);
                $("#dialogshelf").dialog({title:'<{$lang.ws_code_change}>', autoOpen:true});
            }
        }
    });
}

//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>" class="msg"><p align="center">' + tip + '</p></div>').dialog({
        modal:true,
        buttons:{
            '<{$lang.ok}>':function () {
                $(this).dialog("close");
            }
        }
    });
}

function calqty(obj) {
    var this_val = $(obj).val()*1;
    var val= $(obj).attr('oldvalue');//原值
    var pattern = /^[1-9]\d*$/;//匹配正整数
    if (!pattern.test(this_val)) {
        alertTip("<{$lang.must_be_integer}>!");
        $(obj).val(val);
        return;
    }
    if (this_val>val*1) {
        alertTip("<{$lang.move}><{$lang.quantity}>,<{$lang.can_not_more_than}><{$lang.useable}><{$lang.quantity}>!");
        $(obj).val(val);
        return;
    }
}